<%--

  User: PING
  Date: 2022/9/7
  Time: 9:50
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java"
        import="com.javaweb.common.DBUtil"
        import="com.javaweb.common.StringTool"
        import="com.javaweb.common.DispatcherTool"%>
<%@ page import="com.javaweb.entity.Comment" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.concurrent.ConcurrentHashMap" %>
<%@ page import="java.util.ArrayList" %>


<html>
<meta charset="UTF-8">
<head>
    <title></title>
</head>
<body>
    <%
        /**
         *  the JSP is defined to process requests from fav, unfav, comment, ect.
         *
         */
        // process the request from login UI
        request.setCharacterEncoding("UTF-8");
        int tId = StringTool.parseToInt(request.getParameter("tId")) ;
        String type = request.getParameter("type") ;
        String uId = request.getParameter("uId") ;
        String newComment = request.getParameter("newComment") ;

        if ( type == null & ! StringTool.isEmpty(newComment)) {
            // process comment request
            Comment comment = new Comment(tId, StringTool.parseToInt(uId), newComment) ;
            DBUtil.addComment(comment) ;
            Map<Integer, List<Comment>> comMap = (ConcurrentHashMap<Integer, List<Comment>>)application.getAttribute("comMap") ;
            List<Comment> comments = comMap.get(tId) == null ? new ArrayList<Comment>() : comMap.get(tId);
            comments.add(comment) ;
            comMap.put(tId, comments) ;
        } else {
            // process like or unlike request
            if ("like".equals(type)) {
                Map<Integer, Integer> favMap = (ConcurrentHashMap<Integer, Integer>)application.getAttribute("favMap") ;
                favMap.put(tId, favMap.get(tId) + 1) ;
            } else {
                Map<Integer, Integer> unfavMap = (ConcurrentHashMap<Integer, Integer>)application.getAttribute("unfavMap") ;
                unfavMap.put(tId, unfavMap.get(tId) + 1) ;
            }

        }

        response.sendRedirect("show.jsp");

    %>

</body>
</html>
